//
//  ShadowedTextViewGroup.m
//  LineByLine
//
//  Created by 병철 김 on 11. 12. 14..
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "ShadowedTextViewGroup.h"
#import "ShadowedTextView.h"

#define kFontMargin 7
#define kHeightMargin 2

@implementation ShadowedTextViewGroup
@synthesize text, delegate;


- (void) setText:(NSString *)_text {
    [text release];
    text = nil;
    
    text = [_text copy];
    
    [self configView];
}

// TODO 여기서 ShadowedTextView를 Pool로 구성
- (void) configView {
    for (UIView *view in [self subviews]) {
        [view removeFromSuperview];
    }
    
    NSArray *strings = [text componentsSeparatedByString:@" "];
    UIFont *font = [UIFont systemFontOfSize:24];
    
    float posx = 0;
    float posy = 0;
    float margin = kFontMargin;
    
    for (int i=0; i<[strings count]; i++) {
        NSString *string = [strings objectAtIndex:i];
        NSString *realString = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        if ([realString length] <= 0) {
            continue;
        }
        
        CGSize fontSize = [realString sizeWithFont:font];
        if ((posx + fontSize.width + margin) > self.frame.size.width) {
            posy += fontSize.height + kHeightMargin;
            posx = 0;
        }
        
        ShadowedTextView *textView = [[ShadowedTextView alloc] initWithFrame:CGRectMake(posx, posy, fontSize.width, fontSize.height)];
        [textView setDelegate:delegate];
        [textView setText:realString];
        [self addSubview:textView];
        [textView release];
        
        posx += (fontSize.width + margin);
    }
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [delegate onClickText:nil];
}
@end
